<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>酒店房间相册管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">

</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">店房间相册</div>
                <div class="layui-card-body">

                    <div class="layui-form-item"> 
                    </div>

                    <table class="layui-hide" id="test-table-operate" lay-filter="test-table-operate">

                        <script type="text/html" id="is_display_switch">
                            <input type="checkbox" name="is_display" lay-skin="switch" lay-text="上架|下架" lay-filter="is_display_switch"
                                   value="{{ d.is_display }}" data-json="{{ encodeURIComponent(JSON.stringify(d)) }}" {{ d.is_display == 1 ? 'checked' : '' }}>
                        </script>

                    </table>

                    <script type="text/html" id="test-table-operate-barDemo"> 
                        <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="showImg">房间图片</a>
                        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a> 
                    </script>
                </div>
            </div>
        </div>
    </div>
</div> 

<style>
    .d_left{
        width: 150px;
        text-align: right;
        position: relative;
        float: left;
    }

    .d_right{
        width: 300px;
        position: relative;
        float: left;
        padding-left: 20px;
    }

    #detialmodal li{
        margin-bottom: 15px;
    }
</style>

<script src="/static/layuiadmin/layui/layui.js"></script>
<script>  
 layui.use('laydate', function(){
     var laydate = layui.laydate;
      laydate.render({
        elem: '#test4'
        ,type: 'time' 
      }); 
    }); 
 
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index','form','table','laydate','set'], function(){
        $ = layui.jquery;
        var table = layui.table
            ,admin = layui.admin
            ,element = layui.element
            ,layer = layui.layer
            ,laydate = layui.laydate
            ,form = layui.form;

        table.render({
            elem: '#test-table-operate'
            ,url: "{:url('Room/roomKinds')}"
            ,height: 'full-155'
            ,cols: [[
                 {field:'room_id',align:'center',width:200,  title: '房间ID'}
                ,{field:'room_name',align:'center',width:200,  title: '房间名称'}
                ,{field:'room_price',align:'center',   title: '房间价格'}  
                ,{align:'center',width:270, title: '操作',fixed: 'right', toolbar: '#test-table-operate-barDemo'}
           ]]
        });

        form.render(null, 'component-form-group');

        laydate.render({
            elem: '#LAY-component-form-group-date'
        });


        //监听工具条
        table.on('tool(test-table-operate)', function(obj){
            var data = obj.data;
          if(obj.event === 'del'){
                layer.confirm('确认删除房间类型"' + data.room_name + '"吗', function(index){
                    var loading = layer.load(1, {
                        shade: [0.1,'#fff'] //0.1透明度的白色背景
                    });
                    if (data.pid == 0) {
                        layer.msg('主账号禁止删除',{icon:2,time:1000},function(){
                            layer.closeAll();
                        });

                    }else{
                        $.ajax({
                            url: "{:url('Admin/delAdmin')}",
                            type: 'post',
                            dataType: 'json',
                            data: {h_admin_id:data.h_admin_id},
                            success: function(msg){
                                layer.close(loading);
                                if (msg.code === 0) {
                                    obj.del();
                                    layer.close(index);
                                }else if (msg.code === 1){
                                    layer.msg('删除失败',{icon:2,time:1000});
                                }
                            }
                        })
                    }
                });
            } else if(obj.event === 'edit'){  
              window.location.href="{:url('album/roomImages')}?room_id=" + data.room_id ; 
            }else if(obj.event === 'showDetial'){ 
                var loading = layer.load(1, {
                    shade: [0.1,'#fff'] //0.1透明度的白色背景
                });
                var room_id = data.room_id; 
                $.ajax({
                    url: "{:url('Hotel/getRoomDetail')}",
                    type: 'post',
                    dataType: 'json',
                    data: {room_id:room_id},
                    success: function(data){
                        layer.close(loading);
                        if (data.code === 0) { 
                            var obj = data.data;  
                             $('#is_qian_fuwu').html(obj.is_qian_fuwu);
                             $('#is_window').html(obj.is_window);
                             $('#is_wc').html(obj.is_wc);
                             $('#is_wifi').html(obj.is_wifi);
                             $('#is_stop_car').html(obj.is_stop_car);
                             $('#is_qian_fuwu').html(obj.is_qian_fuwu);
                             $('#is_food').html(obj.is_food);
                             $('#room_other').html(obj.room_other);
                             $('#room_fw_other').html(obj.room_fw_other);
                             $('#cancelable_time').html(obj.cancelable_time);
                             $('#room_name').html(obj.room_name);
                             $('#bed_scale').html(obj.bed_scale);
                             $('#square').html(obj.square);
                             $('#available_person').html(obj.available_person);
                             $('#room_name').html(obj.room_name); 
                             $('#room_subject').html(obj.room_subject); 



                            var userDetailModal = layer.open({
                                title:'房间分类详情'
                                ,type: 1
                                ,shadeClose: true
                                ,area:['800px']
                                ,content: $('#detialmodal')
                            });
                        }else{
                            layer.msg('获取信息失败',{icon:2,time:1000});
                        }  
                    }
                })

            }else if(obj.event == 'showImg')
            {  
                 var room_id = data.room_id; 
                $.ajax({
                    url: "{:url('Hotel/viewImage')}",
                    type: 'post',
                    dataType: 'json',
                    data: {room_id:data.room_id},
                    success: function(msg){
                        if (msg.code == 0) {
                            var arr  = msg.data;
                            let img_src_arr = [];
                            for (var i in arr) {
                                img_src_arr.push(arr[i].img_path)
                            }
                            console.log(img_src_arr);
                            var img_src_data = {};
                            img_src_data.title = '景点图片';
                            img_src_data.id = data.id;
                            img_src_data.start = 0;
                            img_src_data.data =[];

                            var src = {};

                            for(var i=0;i<img_src_arr.length;i++){
                                src = {};
                                var num = i+1;
                                src.alt = '景点图片'+ num;
                                src.pid = i;
                                src.src = img_src_arr[i];
                                src.thumb = img_src_arr[i];
                                console.log(src);
                                img_src_data.data.push(src);
                            }
                            layer.photos({
                                photos: img_src_data
                                ,anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
                            });
                        }else{
                            layer.msg(msg.message,{time:1000});
                        }
                    }
                });
            }
        });

        //添加提交
        form.on('submit(addsubmit)', function(data){
            
            if (data.field.is_tehui){

                if (!data.field.room_discount_price){
                    layer.msg('请输入特惠价格',{time:2000});
                    return false;
                }

            }else{

                data.field.is_tehui = 0;

            }
            if (data.field.is_quxiao){

                if (!data.field.cancelable_time){
                    layer.msg('请输入可取消时间',{time:2000});
                    return false;
                }

            }else{

                data.field.is_tehui = 0;

            }

            var loading = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });

            $.ajax({
                url: "{:url('Room/addRoomKinds')}",
                type: 'post',
                dataType: 'json',
                data: {data:data.field},
                success: function(data){
                    if (data.code === 0) {
                        layer.msg('添加成功,请上传房间图片',{icon:1,time:2000},function(){
                            window.location.reload();
                        });
                    }else{
                        layer.close(loading);
                        layer.msg('添加失败',{icon:2,time:1000});
                    }
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //修改提交
        form.on('submit(editsubmit)', function(data){
            var loading = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            $.ajax({
                url: "{:url('Room/editRoomKinds')}",
                type: 'post',
                dataType: 'json',
                data: {data:data.field},
                success: function(info){
                    if (info.code === 0) {
                        layer.msg('修改成功',{icon:1,time:2000},function(){
                            window.location.reload();
                        });
                    }else{
                        layer.close(loading);
                        layer.msg(info.code,{icon:2,time:1000})
                    }
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //开关修改管理员状态
        form.on('switch(is_display_switch)', function(obj){
            var loading = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            var json = JSON.parse(decodeURIComponent($(this).data('json')));
            json = table.clearCacheKey(json);

            var is_display = this.checked ? '1' : '0';

            $.ajax({
                url: "{:url('Room/editRoomKindsDisplay')}",
                type: 'post',
                dataType: 'json',
                data: {room_id:json.room_id,is_display:is_display},
                success: function(info){
                    if (info.code === 0) {
                        layer.close(loading);
                    }else{
                        layer.msg(info.msg,{icon:2,time:1000},function(){
                            window.location.reload();
                        });
                    }
                }
            })
        });

        var $ = layui.$, active = {
            addadmin: function() {
                var addmodal = layer.open({
                    title: '添加房间类型'
                    , type: 1
                    , shadeClose: true
                    , area: ['1100px']
                    , content: $('#addmodal')
                });
            }

        };

        $('.test-table-operate-btn .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });


    //格式化时间函数
    function Format(datetime,fmt) {
        if (parseInt(datetime)==datetime) {
            if (datetime.length==10) {
                datetime=parseInt(datetime)*1000;
            } else if(datetime.length==13) {
                datetime=parseInt(datetime);
            }
        }
        datetime=new Date(datetime);
        var o = {
            "M+" : datetime.getMonth()+1,                 //月份
            "d+" : datetime.getDate(),                    //日
            "h+" : datetime.getHours(),                   //小时
            "m+" : datetime.getMinutes(),                 //分
            "s+" : datetime.getSeconds(),                 //秒
            "q+" : Math.floor((datetime.getMonth()+3)/3), //季度
            "S"  : datetime.getMilliseconds()             //毫秒
        };
        if(/(y+)/.test(fmt))
            fmt=fmt.replace(RegExp.$1, (datetime.getFullYear()+"").substr(4 - RegExp.$1.length));
        for(var k in o)
            if(new RegExp("("+ k +")").test(fmt))
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
        return fmt;
    }

</script>


</body>
</html>